/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn;

import cn.webrx.mapper.*;
import cn.webrx.pojo.Member;
import cn.webrx.pojo.User;
import cn.webrx.pojo.UserAddress;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: mybatis2021 - Demo
 * <p>Powered by webrx On 2021-10-27 11:05:35
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@Slf4j
public class Demo {
    DbMapper dm = null;
    MemberMapper mm = null;
    BookMapper bm = null;
    Connection conn = null;
    UserMapper um = null;
    RoleMapper rm = null;

    @Before
    public void init() {
        //建立数据源 HikariDataSource
        var ds = new HikariDataSource();
        ds.setJdbcUrl("jdbc:mysql:/wxdb");
        ds.setUsername("root");

        //建立事务对象
        var tx = new JdbcTransactionFactory();
        //建立环境对象
        var env = new Environment("development", tx, ds);
        //建立配置对象相当于mybatis-config.xml文件
        var cfg = new Configuration(env);

        //指定别名 java.lang.String 默认的别名说法string
        //指定别名cn.webrx.model.Book  可以直接写book不分区大小写
        var alias = cfg.getTypeAliasRegistry();
        alias.registerAliases("cn.webrx.model");
        alias.registerAliases("cn.webrx.vo");
        alias.registerAliases("cn.webrx.pojo");

        cfg.addMappers("cn.webrx.mapper"); //会自动加载接口类，同时也加载接口同名的同位置的xml

        //加载resouces/mappers/*.xml
        //var xml = "mappers/xyz.xml";
        //var is = Thread.currentThread().getContextClassLoader().getResourceAsStream(xml);
        //System.out.println(is);
        //new XMLMapperBuilder(is, cfg, xml, cfg.getSqlFragments()).parse();


        //会自动批量加载/resources/mappers/*.xml
        var xml = "mappers/";
        var p = Thread.currentThread().getContextClassLoader().getResource(xml).getPath();
        var f = new File(p);
        for (String s : f.list()) {
            if (s.endsWith(".xml")) {
                var t = xml + s;
                var is = Thread.currentThread().getContextClassLoader().getResourceAsStream(t);
                new XMLMapperBuilder(is, cfg, t, cfg.getSqlFragments()).parse();
            }
        }


        var sf = new SqlSessionFactoryBuilder().build(cfg);
        var ss = sf.openSession(true);

        //jdbc 接口
        conn = ss.getConnection();
        dm = ss.getMapper(DbMapper.class);
        bm = ss.getMapper(BookMapper.class);
        um = ss.getMapper(UserMapper.class);
        mm = ss.getMapper(MemberMapper.class);
        rm = ss.getMapper(RoleMapper.class);
    }

    @After
    public void close() {

    }

    @Test
    public void t8(){
        //System.out.println(mm.query());

        //Map<String,Object> map = new HashMap<String, Object>();
        //map.put("account","aa");
        //System.out.println(mm.queryBy(map));

        //System.out.println(rm.queryAll());

        System.out.println(rm.selrole(4));
    }

    @Test
    public void t7(){
        User u = um.showm("root");
        //System.out.println(u);
        System.out.println("-------------------------");
        System.out.println(u.getUserinfo());
        System.out.println("-------------------------");
        u.getUseraddrs().stream().forEach(System.out::println);
        u.getUseraddrs().stream().filter(UserAddress::isDefault).forEach(System.out::println);
    }


    @Test
    public void t6() {
        Map<String, Object> map = new HashMap<String, Object>();
        //map.put("id",300);
        map.put("ids",new int[]{10,20,30,40,50});
        bm.del();
        bm.del(null);
        bm.del(map);
    }

    @Test
    public void t5() {
        Map<String, Object> map = new HashMap<String, Object>();
        //map.put("id",100);
        //map.put("name","%java%");
        //map.put("where","bookid = 33 or book_price>55");

        bm.delteBy(map);
    }

    @Test
    public void t4() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id",100);
        map.put("name","《mysql数据库技术》");
        //bm.addb(map);

        map.put("name","《web2021》");
        map.put("author","aaa");
        map.put("price",55);
        bm.adda(map);
        log.info("{}", bm.queryByName());
    }

    @Test
    public void t3() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", 24);
        //map.put("name", "《java实战开发》");
        map.put("price", 66);
        map.put("author", "abc");
        //bm.update(map);

        map.clear();
        map.put("aprice", "book_price=book_price - 3");
        //bm.update(map);

        log.info("{}", bm.queryByName());
    }

    @Test
    public void t2() {
        log.info("{}", bm.queryByName());

        var map = new HashMap<String, Object>();
        log.info("{}", bm.queryByName(null));

        map.put("name", "%技术%");
        map.put("fields", "book_id,book_name");
        log.info("{}", bm.queryByName(map));

        map.clear();
        map.put("fields", "book_name");
        map.put("order", "book_id desc");
        log.info("{}", bm.queryByName(map));

    }

    @Test
    public void t1() {
        log.info("{}", dm.tbs());
        //log.info("{}", bm.queryAll());
        //log.info("{}", bm.show());


        Map<String, Object> map = new HashMap<String, Object>();
        map.put("flag", "student");
        //log.info("{}", bm.myshow(null));
        //log.info("{}", bm.myshow(map));


        dm.tbs().forEach(e -> {
            map.put("flag", e);
            log.info("{}", bm.myshow(map));
        });


    }
}
